package com.zhxt.web.controller.zhxtapi;

import com.zhxt.common.annotation.Log;
import com.zhxt.common.core.controller.BaseController;
import com.zhxt.common.core.domain.AjaxResult;
import com.zhxt.common.core.domain.entity.SysDept;
import com.zhxt.common.core.page.TableDataInfo;
import com.zhxt.common.core.text.Convert;
import com.zhxt.common.enums.BusinessType;
import com.zhxt.system.domain.ZhxtFiles;
import com.zhxt.system.domain.ZhxtFilesLookRecord;
import com.zhxt.system.domain.ZhxtSourceInfo;
import com.zhxt.system.service.ISysDeptService;
import com.zhxt.system.service.IZhxtFilesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 管理系统-文件管理
 */
@RestController
@RequestMapping("/zhxtapi/files")
public class FilesAdminController extends BaseController {
    @Autowired
    private IZhxtFilesService iZhxtFilesService;

    @Autowired
    private ISysDeptService deptService;
    /**
     * 查询管理制度列表
     */
    @PostMapping("/getListM")
    public TableDataInfo getListM(ZhxtFiles zhxtFiles){
        startPage();
        zhxtFiles.setSourcestr("1");
        List<ZhxtFiles> zhxtFilesList=iZhxtFilesService.getListM(zhxtFiles);
        return getDataTable(zhxtFilesList==null?new ArrayList<>():zhxtFilesList);
    }

    /**
     * 查询管理制度详情
     */
    @PostMapping("/getMInfo")
    public AjaxResult getMInfo(String contnum){
        AjaxResult ajax=AjaxResult.success();
        List<ZhxtSourceInfo> zhxtSourceInfoList=iZhxtFilesService.getMInfo(contnum);
        ajax.put("data",zhxtSourceInfoList);
        List<ZhxtSourceInfo> zhxtSourceInfoListf=iZhxtFilesService.getFInfo(contnum);
        ajax.put("fdata",zhxtSourceInfoListf);
        return ajax;
    }

    /**
     * 查询电子期刊列表
     */
    @PostMapping("/getListT")
    public TableDataInfo getListT(ZhxtFiles zhxtFiles){
        startPage();
        List<ZhxtFiles> zhxtFilesList=iZhxtFilesService.getListT(zhxtFiles);
        return getDataTable(zhxtFilesList);
    }

    /**
     * 查询电子期刊详情
     */
    @PostMapping("/getTInfo")
    public AjaxResult getTInfo(String contnum){
        AjaxResult ajax=AjaxResult.success();
        List<ZhxtSourceInfo> zhxtSourceInfoList=iZhxtFilesService.getTInfo(contnum);
        ajax.put("data",zhxtSourceInfoList);
        return ajax;
    }

    /**
     * 添加管理制度
     */
    @Log(title = "pc-新增管理制度", businessType = BusinessType.INSERT)
    @PostMapping("addMFiles")
    public AjaxResult addMFiles(ZhxtFiles zhxtFiles){
        AjaxResult ajax=AjaxResult.success();
        int rNum=iZhxtFilesService.addMFiles(zhxtFiles);
        if(rNum > 0){
            ajax.put("dcode","00");
            ajax.put("dresult","新增成功");
        }else{
            ajax.put("dcode","99");
            ajax.put("dresult","新增失败");
        }
        return ajax;
    }

    //管理制度添加附件
    @Log(title = "pc-新增管理制度附件", businessType = BusinessType.INSERT)
    @PostMapping("addMFilesf")
    public AjaxResult addMFilesf(ZhxtSourceInfo zhxtSourceInfo){
        AjaxResult ajax=AjaxResult.success();
        zhxtSourceInfo.setCtype("F");//附件类型
        int aNum=iZhxtFilesService.addMFilesf(zhxtSourceInfo);
        if(aNum > 0){
            ajax.put("dcode","00");
            ajax.put("dresult","新增成功");
        }else{
            ajax.put("dcode","99");
            ajax.put("dresult","新增失败");
        }
        return ajax;
    }


    /**
     * 添加电子期刊
     */
    @Log(title = "pc-新增电子期刊", businessType = BusinessType.INSERT)
    @PostMapping("addTFiles")
    public AjaxResult addTFiles(ZhxtFiles zhxtFiles){
        AjaxResult ajax=AjaxResult.success();
        int rNum=iZhxtFilesService.addTFiles(zhxtFiles);
        if(rNum > 0){
            ajax.put("dcode","00");
            ajax.put("dresult","新增成功");
        }else{
            ajax.put("dcode","99");
            ajax.put("dresult","新增失败");
        }
        return ajax;
    }

    /**
     * 删除管理制度
     */
    @Log(title = "pc-删除管理制度", businessType = BusinessType.DELETE)
    @PostMapping("delMFiles")
    public AjaxResult delMFiles(ZhxtFiles zhxtFiles){
        AjaxResult ajax=AjaxResult.success();
        int delNum=iZhxtFilesService.delFile(Convert.toStr(zhxtFiles.getContnum()));
        if(delNum > 0){
            ajax.put("dcode","00");
            ajax.put("dresult","删除成功");
        }else{
            ajax.put("dcode","99");
            ajax.put("dresult","删除失败");
        }
        return ajax;
    }

    /**
     * 删除管理制度附件
     */
    @Log(title = "pc-删除管理制度附件", businessType = BusinessType.INSERT)
    @PostMapping("delMFilesf")
    public AjaxResult delMFilesf(ZhxtSourceInfo zhxtSourceInfo){
        AjaxResult ajax=AjaxResult.success();
        int delNum=iZhxtFilesService.delFilef(zhxtSourceInfo);
        if(delNum > 0){
            ajax.put("dcode","00");
            ajax.put("dresult","删除成功");
        }else{
            ajax.put("dcode","99");
            ajax.put("dresult","删除失败");
        }
        return ajax;
    }

    /**
     * 删除电子期刊
     */
    @Log(title = "pc-删除电子期刊", businessType = BusinessType.DELETE)
    @PostMapping("delTFiles")
    public AjaxResult delTFiles(ZhxtFiles zhxtFiles){
        AjaxResult ajax=AjaxResult.success();
        int delNum=iZhxtFilesService.delFile(Convert.toStr(zhxtFiles.getContnum()));
        if(delNum > 0){
            ajax.put("dcode","00");
            ajax.put("dresult","删除成功");
        }else{
            ajax.put("dcode","99");
            ajax.put("dresult","删除失败");
        }
        return ajax;
    }

    /**
     * 管理制度审核
     */
    @Log(title = "pc-管理制度审批", businessType = BusinessType.GRANT)
    @PostMapping("spMFile")
    public AjaxResult spMFile(ZhxtFiles zhxtFiles){
        AjaxResult ajax=AjaxResult.success();
        int rNum=iZhxtFilesService.spMFile(zhxtFiles);
        if(rNum > 0){
            ajax.put("dcode","00");
            ajax.put("dresult","操作成功");
        }else{
            ajax.put("dcode","99");
            ajax.put("dresult","操作失败");
        }
        return ajax;
    }

    /**
     * 修改管理制度
     * 修改排序和状态
     */
    @Log(title = "pc-修改管理制度", businessType = BusinessType.UPDATE)
    @PostMapping("updateMFile")
    public AjaxResult updateMFile(ZhxtFiles zhxtFiles){
        AjaxResult ajax=AjaxResult.success();
        zhxtFiles.setCtype("M");
        int uNum=iZhxtFilesService.updateFile(zhxtFiles);
        if(uNum > 0){
            ajax.put("dcode","00");
            ajax.put("dresult","修改成功");
        }else{
            ajax.put("dcode","99");
            ajax.put("dresult","修改失败");
        }
        return ajax;
    }

    /**
     * 修改中原太保
     * 修改排序和状态
     */
    @Log(title = "pc-修改中原太保", businessType = BusinessType.UPDATE)
    @PostMapping("updateTFile")
    public AjaxResult updateTFile(ZhxtFiles zhxtFiles){
        AjaxResult ajax=AjaxResult.success();
        zhxtFiles.setCtype("T");
        zhxtFiles.setFilestatus("0");
        int uNum=iZhxtFilesService.updateFile(zhxtFiles);
        if(uNum > 0){
            ajax.put("dcode","00");
            ajax.put("dresult","修改成功");
        }else{
            ajax.put("dcode","99");
            ajax.put("dresult","修改失败");
        }
        return ajax;
    }

    /**
     * 查询部门列表
     */
    @PostMapping("getDeptList")
    public AjaxResult getDeptList(){
        AjaxResult ajax=AjaxResult.success();
        SysDept dept=new SysDept();
        dept.setParentId(101L);
        dept.setStatus("0");
        List<SysDept> depts = deptService.selectMDeptList(dept);
        ajax.put("data",depts);
        return ajax;
    }

    /**
     * 查询浏览流水
     */
    @PostMapping("getMLookList")
    public TableDataInfo getMLookList(String contnum){
        startPage();
        List<ZhxtFilesLookRecord> zhxtFilesLookRecordList=iZhxtFilesService.getMLookList(contnum);
        return getDataTable(zhxtFilesLookRecordList==null?new ArrayList<ZhxtFilesLookRecord>():zhxtFilesLookRecordList);
    }

}
